package com.lxj.controller;

import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.lxj.config.ResourceServerConfig;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author lxj
 * @date 2021/8/1 10:06
 * @Description:
 */
@RestController
@Api(tags = "文件上传的管理")
@RequestMapping("admin/file")
public class FileController {

    @Autowired
    private FastFileStorageClient fastFileStorageClient;

    @Autowired
    private ResourceServerConfig resourceServerConfig;


    @PostMapping("upload/element")
    @ApiOperation("文件上传")
    public ResponseEntity<String> uploadFile(@RequestParam MultipartFile file) {
        String originalFilename = file.getOriginalFilename();
        String ext = StringUtils.getFilenameExtension(originalFilename);
        long size = file.getSize();
        InputStream inputStream = null;
        try {
            inputStream = file.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        String fullPath = fastFileStorageClient.uploadFile(inputStream, size, ext, null).getFullPath();
        return ResponseEntity.ok(resourceServerConfig.getServerPath() + fullPath);
    }

}


